********************************************************************************
** Do file: g4_endline_educ_indiv.do
** First started: February 19, 2023
** Last edited: December 31, 2023

/* Purpose: this do file analyzes education outcomes at the individual level

* ITEM MADE: TABLE 4
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Preamble - data management for both the individual level dataset and household level dataset **

** Individual **

* Load key data first
use "$merged/key_rep.dta", clear
keep if hhid!=.

* Reduce down to strict sample - sampling framework variant
keep if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

* Bring in individual level information on education
merge 1:m hhid using "$esvy_clean/2_educ_indiv.dta"
keep if _merge==3 | _merge==1
drop _merge
		
/* Change school expenditures value to USD (2019) https://data.worldbank.org/indicator/PA.NUS.FCRF?locations=UG */
replace schoolexpend_t2_fin = schoolexpend_t2_fin/3704
		
* Take log
g ln_schoolexpend_t2_fin=ln(schoolexpend_t2_fin+1)

* Drop the choice group as well
drop if treatmenttype_sh=="R T3"

* Generate treatment variables
g locked_assigned = (treatmenttype_sh=="R T1-L")
g surprise_assigned = (treatmenttype_sh=="R T1-U")
g unlocked_assigned = (treatmenttype_sh=="R T2-U")
g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
		
* Develop a num_520 variable but for endline
g counter = 1
bys hhid: egen num_520e = sum(counter)
drop counter

* Temporarily save data
tempfile indiv
save `indiv'



************
** Part I **
************

use `indiv', clear

* Consistency
replace missed_month_t2_fin = . if ln_schoolexpend_t2_fin==.

g male_d = (fem_fin==0)
g female_d = (fem_fin==1)
g male_at = male_d*anytreat_assigned
g female_at = female_d*anytreat_assigned

* Generate interaction term with num_520e
g atreatnum_520e = anytreat_assigned * num_520e

* Input zeroes if missing for days absent, zeroes anything else
count if missed_month_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 545
count if ln_schoolexpend_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 545
replace missed_month_t2_fin=30 if anytreat_assigned!=. & enroll_t2_fin!=. & missed_month_t2_fin==.
replace schoolexpend_t2_fin=0 if anytreat_assigned!=. & enroll_t2_fin!=. & ln_schoolexpend_t2_fin==.
replace ln_schoolexpend_t2_fin=0 if anytreat_assigned!=. & enroll_t2_fin!=. & ln_schoolexpend_t2_fin==.

merge 1:1 hhid person using "$esvy_clean/maleeduc.dta", keepusing(outcome_h1)
keep if _merge==1 | _merge==3
ren outcome_h1 outcome_h1_fin
drop _merge

merge 1:1 hhid person using "$esvy_clean/femaleeduc.dta", keepusing(outcome_h1)
keep if _merge==1 | _merge==3
replace outcome_h1_fin=outcome_h1 if outcome_h1_fin==.

*** Education Outcomes for School Aged Children (ITT)

***** TABLE 4 *****

* Outcome control means for males and females
sum enroll_t2_fin if fem_fin==0 & anytreat_assigned==0
sum enroll_t2_fin if fem_fin==1 & anytreat_assigned==0
sum missed_month_t2_fin if fem_fin==0 & anytreat_assigned==0
sum missed_month_t2_fin if fem_fin==1 & anytreat_assigned==0
sum schoolexpend_t2_fin if fem_fin==0 & anytreat_assigned==0
sum schoolexpend_t2_fin if fem_fin==1 & anytreat_assigned==0
sum outcome_h1_fin if fem_fin==0 & anytreat_assigned==0
sum outcome_h1_fin if fem_fin==1 & anytreat_assigned==0

reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==0, cluster(hhid)
nlcom exp(_b[anytreat_assigned]+3*_b[atreatnum_520e])-1
* Note: the 34% for males mentioned in the paper comes from the rounded Table figures

reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==1, cluster(hhid)
nlcom exp(_b[anytreat_assigned]+3*_b[atreatnum_520e])-1
* Note: the 20% for females mentioned in the paper comes from the rounded Table figures

* OLS results

eststo: reg enroll_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==0, cluster(hhid)
eststo: reg enroll_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==1, cluster(hhid)

eststo: reg missed_month_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==0, cluster(hhid)
eststo: reg missed_month_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==1, cluster(hhid)

eststo: reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==0, cluster(hhid)
eststo: reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==1, cluster(hhid)

eststo: reg outcome_h1_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==0, cluster(hhid)
eststo: reg outcome_h1_fin anytreat_assigned atreatnum_520e num_520e if fem_fin==1, cluster(hhid)

esttab using "$tables/endline_educ_indiv.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Education Outcomes for School-Aged Children\label{endline_educ_indiv}) ///
	label noconstant nodepvars mtitles("Enrollment" "Enrollment" "Days absent" "Days absent" "Log school expenditures" "Log school expenditures" "Education index" "Education index") stats(N,labels ("n")) nonotes ///
		varlabels(locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" anytreat_assigned "Pooled" _cons "Constant" num_520e "N SAC at endline" atreatnum_520e "Pooled $\times$ Number of School-Aged Children", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace atreatnum_520e \addlinespace num_520e \addlinespace))
eststo clear

* Test equality of coefficients
g atreatnum_520e_m = atreatnum_520e*male_d
g atreatnum_520e_f = atreatnum_520e*female_d
g num_520e_m = num_520e*male_d
g num_520e_f = num_520e*female_d

reg enroll_t2_fin male_d male_at female_d female_at atreatnum_520e_m atreatnum_520e_f num_520e_m num_520e_f if fem_fin!=., cluster(hhid) noconstant
test _b[male_at] = _b[female_at]

reg missed_month_t2_fin male_d male_at female_d female_at atreatnum_520e_m atreatnum_520e_f num_520e_m num_520e_f if fem_fin!=., cluster(hhid) noconstant
test _b[male_at] = _b[female_at]

reg ln_schoolexpend_t2_fin male_d male_at female_d female_at atreatnum_520e_m atreatnum_520e_f num_520e_m num_520e_f if fem_fin!=., cluster(hhid) noconstant
test _b[male_at] = _b[female_at]

reg outcome_h1_fin male_d male_at female_d female_at atreatnum_520e_m atreatnum_520e_f num_520e_m num_520e_f if fem_fin!=., cluster(hhid) noconstant
test _b[male_at] = _b[female_at]
